//
//  BLSlider.m
//  ITC_iOS
//
//  Created by Benz.L on 06/09/2018.
//  Copyright © 2018 Benz.L. All rights reserved.
//

#import "BLSlider.h"
#import "UIImage+Create.h"
#import "UIColor+Create.h"

@interface BLSliderFlag : UIView {
    UIImageView *_imv;
    UILabel *_label;
}
@property(nonatomic, strong) NSString *title;
@end
@implementation BLSliderFlag
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"移动标签"]];
        [_imv sizeToFit];
        _label = [UILabel new];
        _label.font = [UIFont systemFontOfSize:10];
        _label.textColor = [UIColor colorWithHexValue:0xffffff];
        _label.textAlignment = NSTextAlignmentCenter;
        CGRect frame = _imv.bounds;
        frame.size.height -= 2;
        _label.frame = frame;
        
        self.frame = _imv.bounds;
        [self addSubview:_imv];
        [self addSubview:_label];
    }
    return self;
}
- (void)setTitle:(NSString *)title {
    _title = title;
    _label.text = title;
}

@end


@interface BLSlider() {
    BLSliderFlag *_flag;
}
@property(nonatomic, weak) UIImageView *thumb;
@end
@implementation BLSlider

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configUI];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configUI];
}

- (CGRect)trackRectForBounds:(CGRect)bounds {
    CGFloat width = CGRectGetWidth(bounds);
    CGFloat y = (CGRectGetHeight(bounds)-8)/2.0;
    return  (CGRect){0, y, width, 8};
}
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {
    CGRect frame = [super thumbRectForBounds:bounds trackRect:rect value:value];
    CGPoint point = _flag.center;
    point.x = CGRectGetWidth(frame)/2.0+CGRectGetMinX(frame);
    _flag.center = point;
    _flag.title = BLocalizeFormat(@"%@'月", @((int)value));
    return frame;
}

- (void)configUI {
    _flag = [BLSliderFlag new];
    CGRect frame = _flag.frame;
    frame.origin.y = -CGRectGetHeight(frame)-2;
    _flag.frame = frame;
    
    [self addSubview:_flag];
    UIImage *gradents = [UIImage imageWithGradients:@[(id)[UIColor colorWithHexValue:0x22C7F5].CGColor, (id)[UIColor colorWithHexValue:0x4170DD].CGColor] size:(CGSize){1,1}];
    UIImage *maxTrack = [UIImage imageWithColor:[UIColor colorWithHexValue:0x1e2039]];
    UIImage *thumbImg = [UIImage imageWithColor:[UIColor colorWithHexValue:0x4170DD] size:(CGSize){16, 16}];
    [self setMinimumTrackImage:gradents forState:UIControlStateNormal];
    [self setMaximumTrackImage:maxTrack forState:UIControlStateNormal];
    [self setThumbImage:thumbImg forState:UIControlStateNormal];
    //self.minimumTrackTintColor = [UIColor colorWithHexValue:0x22C7F5];
    //self.maximumTrackTintColor = [UIColor colorWithHexValue:0x1e2039];
    //self.thumbTintColor = [UIColor colorWithHexValue:0x4170DD];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.layer.masksToBounds = YES;
        if (obj!=self.subviews.lastObject) {
            obj.layer.cornerRadius = 4;
        }
        else {
            obj.layer.cornerRadius = CGRectGetHeight(obj.frame)/2.0;
        }
    }];
}

@end
